package com.wsoft.form.manager;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.form.entity.WfFormDesignEntity;
import com.wsoft.form.mapper.WfFormDesignMapper;
import com.wsoft.form.query.design.DesignListQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/8/30 13:35
 */
@Service
public class WfFormDesignManager extends ServiceImpl<WfFormDesignMapper, WfFormDesignEntity> {

    @Resource
    private RedisService redisService;

    /**
     * 根据 formDesignCode 查询
     *
     * @param formDesignCode code
     * @return entity
     */
    public WfFormDesignEntity getByFormDesignCode(String formDesignCode) {
        QueryWrapper<WfFormDesignEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormDesignEntity::getFormDesignCode, formDesignCode);
        return getOne(query);
    }

    /**
     * 根据id查询
     *
     * @param designId
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Form.DESIGN_INFO,
            expire_sec = RedisKeyConstant.Form.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public WfFormDesignEntity getById(Long designId) {
        return super.getById(designId);
    }

    /**
     * 保存
     *
     * @param entity
     * @return
     */
    @Override
    public boolean save(WfFormDesignEntity entity) {
        boolean b = super.save(entity);
        if(b){
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.DESIGN_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 根据id修改
     *
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(WfFormDesignEntity entity) {
        boolean b = super.updateById(entity);
        if (b) {
            redisService.del(RedisKeyConstant.Form.DESIGN_INFO  + entity.getId());
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.DESIGN_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 根据id删除
     *
     * @param designId
     * @return
     */
    public boolean removeById(Long designId) {
        boolean b = super.removeById(designId);
        if (b) {
            redisService.del(RedisKeyConstant.Form.DESIGN_INFO + designId);
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.DESIGN_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 查询form下的 design表
     * @param param
     * @return
     */
    public List<WfFormDesignEntity> getByFormId(DesignListQuery param) {
        QueryWrapper<WfFormDesignEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormDesignEntity::getFormId,param.getFormId())
                .like(StrUtil.isNotBlank(param.getFormDesignName()),WfFormDesignEntity::getFormDesignName,param.getFormDesignName())
                .like(StrUtil.isNotBlank(param.getFormDesignCode()),WfFormDesignEntity::getFormDesignCode,param.getFormDesignCode());
        return list(query);
    }

    public String test(){
        return RandomUtil.randomString(10);
    }
}
